/**
 * 
 */
package com.lehecai.ucenter.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lehecai.ucenter.utils.PageBean;

/**
 * @author chirowong
 *
 */
public class QueryParam {
	
	private String entityName;
	private String entityAlias = QueryConstant.DEFAULT_ENTITY_ALIAS;//实体类别名
	
	private Map<String, QueryWhere> paramsWhere;
	private List<QueryOrder> orders;
	private PageBean pageBean;
	private List<QueryField> fields; //用于汇总或分组字段
	
	public String getEntityName() {
		return entityName;
	}
	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}
	
	public String getEntityAlias() {
		return entityAlias;
	}
	public void setEntityAlias(String entityAlias) {
		this.entityAlias = entityAlias;
	}
	
	public Map<String, QueryWhere> getParamsWhere() {
		return paramsWhere;
	}
	
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	
	public List<QueryField> getFields() {
		if(fields != null){
			return Collections.unmodifiableList(fields);
		}else{
			return null;
		}
	}

	public List<QueryOrder> getOrders() {
		if(orders != null){
			return Collections.unmodifiableList(orders);
		}else{
			return null;
		}
	}
	
	public void setParameter(String key, Object value) {
		this.setParameter(key, value, QueryConstant.QUERY_OP_EQUAL);
	}
	
	public void setParameter(String key, Object value, String op) {
		QueryWhere where = new QueryWhere(key, value, op);
		this.setParameter(key, where);
	}
	
	public void setParameter(String paramKey, String whereKey, Object value, String op) {
		QueryWhere where = new QueryWhere(whereKey, value, op);
		this.setParameter(paramKey, where);
	}
	
	public void setParameterIn(String key, List<String> values) {
		QueryWhere where = new QueryWhere(key, values);
		this.setParameter(key, where);
	}
	
	public void setParameter(String key, QueryWhere where) {
		if (paramsWhere == null) {
			synchronized (this) {
				if (paramsWhere == null) {
					paramsWhere = new HashMap<String, QueryWhere>();
				}
			}
		}
		paramsWhere.put(key, where);
	}
	
	public void setParameterLike(String key, Object value) {
		this.setParameter(key, value, QueryConstant.QUERY_OP_LIKE);
	}
	
	public void setParameterLess(String key, Object value) {
		if (value != null) {
			setParameter(key + QueryConstant.QUERY_OP_BETWEEN_END, key, value, QueryConstant.QUERY_OP_LESS);
		}
	}
	
	public void setParameterGreater(String key, Object value) {
		if (value != null) {
			setParameter(key + QueryConstant.QUERY_OP_BETWEEN_START, key, value, QueryConstant.QUERY_OP_GREATER);
		}
	}
	
	public void setParameterBetween(String key, Object lowVal, Object highVal) {
		if (lowVal != null) {
			setParameter(key + QueryConstant.QUERY_OP_BETWEEN_START, key, lowVal, QueryConstant.QUERY_OP_GREATER_AND_EQUAL);
		}
		if (highVal != null) {
			setParameter(key + QueryConstant.QUERY_OP_BETWEEN_END, key, highVal, QueryConstant.QUERY_OP_LESS_AND_EQUAL);
		}
	}
	
	public void addOrder(String field) {
		this.addOrder(field, QueryConstant.QUERY_ORDER_ASC);
	}
	
	public void addOrder(String field, String orderIndex) {
		QueryOrder order = new QueryOrder(field, orderIndex);
		this.addOrder(order);
	}
	
	private void addOrder(QueryOrder order) {
		if (orders == null) {
			synchronized (this) {
				if (orders == null) {
					orders = new ArrayList<QueryOrder>();
				}
			}
		}
		orders.add(order);
	}
	
	public void addField(String field){
		QueryField queryField = new QueryField(field);
		this.addField(queryField);
	}
	
	private void addField(QueryField queryField){
		if(fields == null){
			synchronized(this){
				if(fields == null){
					fields = new ArrayList<QueryField>();
				}
			}
		}
		fields.add(queryField);
	}
}
